import Cookies from 'js-cookie'

const TokenKey = 'userCode'
const userCodeKey = 'userName'
const userInfoKey = 'userInfo'
const mainRoutes = 'mainRoutes'
const roleBtnsKey = 'roleBtns'

// 用户登录token
export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  Cookies.set(TokenKey, token)
}

export function removeToken() {
  Cookies.remove(TokenKey)
}

// 用户登录名
export function getUserCode() {
  return Cookies.get(userCodeKey)
}

export function setUserCode(userCode) {
  Cookies.set(userCodeKey, userCode)
}

export function removeUserCode() {
  Cookies.remove(userCodeKey)
}

// 用户基本信息 包含手机号头像部门名称等
export function getUserInfo() {
  let temp = sessionStorage.getItem(userInfoKey)
  if (!temp) return {}
  temp = JSON.parse(temp)
  return temp
}

export function setUserInfo(userInfo) {
  let temp = ''
  if (typeof routes != 'string') {
    temp = JSON.stringify(userInfo)
  }
  sessionStorage.setItem(userInfoKey, temp)
}

export function removeUserInfo() {
  sessionStorage.removeItem(userInfoKey)
}

// 后台获取的路由信息
export function getMainRoutes() {
  let routes = sessionStorage.getItem(mainRoutes)
  if (!routes) return []
  routes = JSON.parse(routes)
  return routes
}

export function setMainRoutes(routes) {
  let temp = ''
  if (typeof routes != 'string') {
    temp = JSON.stringify(routes)
  }
  sessionStorage.setItem(mainRoutes, temp)
}

export function removeMainRoutes() {
  sessionStorage.removeItem(mainRoutes)
}

// 登录获取到的权限按钮
export function getRoleBtns() {
  let roleBtns = sessionStorage.getItem(roleBtnsKey)
  if (!roleBtns) return []
  roleBtns = JSON.parse(roleBtns)
  return roleBtns
}

export function setRoleBtns(roleBtns) {
  let temp = ''
  if (typeof roleBtns != 'string') {
    temp = JSON.stringify(roleBtns)
  }
  sessionStorage.setItem(roleBtnsKey, temp)
}

export function removeRoleBtns() {
  sessionStorage.removeItem(roleBtnsKey)
}

// 工具函数---------------------------------

// 合成动态路由信息
export function makeRoutesTool(data, asyncRouterMap) {
  let baseroute = []
  asyncRouterMap.forEach(item => {
    data.forEach(items => {
      if (item.path === items.path) { // 一级路由对比path
        if (items.children && items.children.length === 1) {
          item.alwaysShow = true
        }
        item.meta.title = items.meta && items.meta.title ? items.meta.title : item.meta.title
        item.meta.menuCode = items.path.slice(1) // 权限按钮code
        // item.meta.icon = items.meta.icon
        const arr = []
        item.children.forEach(childItem => { // 二级路由对比name
          items.children.forEach(childItems => {
            if (childItem.name === childItems.name) {
              childItem.meta.title = childItems.meta && childItems.meta.title ? childItems.meta.title : childItem.meta.title
              childItem.meta.menuCode = childItems.path // 权限按钮code
              // childItem.meta.icon = childItems.meta.icon
              arr.push(childItem)
            }
          })
        })
        item.children = arr
      }
    })
  })
  baseroute = asyncRouterMap
  baseroute = baseroute.filter(item => item.children && item.children.length > 0) // 过滤出没有二级路由的项
  return baseroute
}
// 合成权限按钮数据
export function makeRoleBtns(roleBtns) {
  if (!roleBtns) return {}
  const btnNames = {
    add: '新增',
    edit: '修改',
    del: '删除',
    list: '列表'
  }
  return roleBtns.reduce((o, n) => {
    const reg = /([0-9a-zA-Z]+)/g
    const arr = n.match(reg)
    if (arr && arr.length && arr.length === 2) {
      const [key, val] = arr
      if (val !== 'list') { // 排除list类型的按钮
        if (!o[key]) {
          o[key] = []
        }
        o[key].push({ funCode: val, funName: btnNames[val] })
      }
    }
    return o
  }, {})
}
